package com.toilet.easyvideofx.manager;

import com.toilet.easyvideofx.api.response.FileResponse;
import com.toilet.easyvideofx.api.response.FileStreamResponse;
import com.toilet.easyvideofx.compent.RequestComponent;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

import java.io.*;

public class DownloadService extends Service<Double> {
    /**
     * 开始大小
     */
    private Double startSize;

    /**
     * 请求下载的id
     */
    private Long downLoadId;

    public DownloadService(Double startSize, Long downLoadId) {
        this.startSize = startSize;
        this.downLoadId = downLoadId;
    }

    @Override
    protected Task<Double> createTask() {
        FileStreamResponse streamResponse = RequestComponent.downloadStream(null, "/test/download");
        InputStream stream = streamResponse.stream();
        long total = 0;
        try (RandomAccessFile raf = new RandomAccessFile("E:\\迅雷云盘\\" + streamResponse.fileName(), "rw")) {
            byte[] bytes = stream.readAllBytes();
            total += bytes.length;

            raf.write(stream.readAllBytes());
            raf.seek(0); // 定位到写入数据的末尾
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Task<>() {
            @Override
            protected Double call() throws InterruptedException {
//                updateProgress(, streamResponse.fileSize());
                return startSize;
            }
        };
    }
}
